<?php
########################################################################################
# Empresa :     Tecnopac Informatica;                                                  #
# Autor:        Deiverson Pacheco;                                                     #
# Data Criação: 10/12/2010;                                                            #
########################################################################################
session_start();
header("Cache-control: private");
include_once("../../includes/session.php");


//Linha para formatação de caracteres com acentuação
header("Content-Type: text/html; charset=ISO-8859-1",true);


//======================================================================================
//==> Include obrigatórias no script
//======================================================================================
include_once('../../includes/classes/class.TemplatePower.php');
include_once('../../includes/classes/class.Conexao.php');
include_once('../../includes/classes/class.Geral.php');

//======================================================================================
//==> Inicializar Classes
//======================================================================================
$not  = new Persist;
$not2 = new Persist;
$data = new getData;
$cor  = new ColorTr;
$util = new Util;
$vld  = new ValidarDados;
$sql  = new SqlInjection;

//======================================================================================
//==> Instanciar TemplatePower para juntar o PHP + HTML
//======================================================================================
$tpl  = new TemplatePower('../../html/sistema/usuarios_man.html');
$tpl->prepare();


//======================================================================================
//==> Recebe variaveis de pesquisa
//======================================================================================
$id          = @$_GET['id'];
$evento      = @$_GET['evento'];


if($evento != 'editar'){
	$tpl->assign('botao','<input type="button" value="Salvar novo" onClick="salvar_usuarios();">');
	$tpl->assign('botao3','<input type="button" value="Voltar a consulta" onClick="volta_list_usuarios();">');
}else{
	$tpl->assign('botao3','<input type="button" value="Voltar a consulta" onClick="volta_list_usuarios();">');
	$tpl->assign('botao','<input type="button" value="Salvar altera&ccedil;&atilde;o" onClick="salvar_edit_usuarios();">');
}

//======================================================================================
//==> Executa case conforme evento
//======================================================================================

if(isset($evento)){
	switch($evento){
		
		case 'salvar': {
		  	$nome  = $sql->tratar($_GET['nome'],100);
		  	$login = $sql->tratar($_GET['login'],100);
		  	$senha = $sql->tratar($_GET['senha'],100);
		  	$email = $sql->tratar($_GET['email'],100);
		  	$guser = $sql->tratar($_GET['guser'],100);

			//captura data e hora atual
			$dt = $data->getDataHoje('-').' '.$data->getHora();
			
			$not->sql = "insert into usuarios
						(nome_completo, login, senha, id_grupo, email, ativo, uid, data_hora) 
						 
						 values 
						
						('$nome', '$login', '$senha', '$guser', '$email', 'Sim', '$ID_USUARIO','$dt')";
			$not->ins();
			
			$not->sql = "SELECT MAX(id) AS id FROM usuarios";
			$not->ler();
			$not->ini();
			
			print "php/sistema/usuarios_man.php?evento=editar&id=$not->id";
			exit;
		break;
		}
		
		
		case 'editar': {
		  	$id = $sql->tratar($id,10);
			
			$not->sql = "select usuarios.id, usuarios.nome_completo, usuarios.login, usuarios.senha, usuarios.email,
						 usuarios.id_grupo, grupos_usuarios.grupo 
						 from usuarios inner join grupos_usuarios 
						 on usuarios.id_grupo = grupos_usuarios.id
						 where usuarios.id = '$id'";
			if($not->ler()){
			   $not->ini();
			   $tpl->assign('id',$not->id);
			   $tpl->assign('nome',$not->nome_completo);
			   $tpl->assign('login',$not->login);
			   $tpl->assign('senha',$not->senha);
			   $tpl->assign('csenha',$not->senha);
			   $tpl->assign('email',$not->email);
			   $tpl->assign('idguser',$not->id_grupo);
			   $tpl->assign('guser',$not->grupo);
			}

		break;
		}
		
		

		case 'salvar_edit': {
		  	$nome  = $sql->tratar($_GET['nome'],100);
		  	$login = $sql->tratar($_GET['login'],100);
		  	$senha = $sql->tratar($_GET['senha'],100);
		  	$email = $sql->tratar($_GET['email'],100);
		  	$guser = $sql->tratar($_GET['guser'],3);
			
			//captura data e hora atual
			$dt = $data->getDataHoje('-').' '.$data->getHora();
			
			$not->sql = "Update usuarios set 
			nome_completo = '$nome',
			login         = '$login',
			senha         = '$senha',
			email         = 'email',
			id_grupo      = '$guser'
			where id = '$id'";
			
			$not->alt();
			
			
			print "php/sistema/usuarios_man.php?evento=editar&id=$id";
			exit;

		break;
		}


		case 'excluir': {
			$codigos = explode(";",$id);
			for($i=1;count($codigos)>$i;$i++){ 
				$not->sql = "update usuarios set ativo='Nao' where id = '$codigos[$i]'";
				$not->alt();
			}		
			print "php/sistema/usuarios.php";
			exit;
		break;
		}
		
		
		
	}
}



//======================================================================================
//==> Enviar página unificada PHP + HTML
//======================================================================================
$tpl->showUnAssigned( false );  // para debug. na hora de publicar, setar para false
$tpl->printToScreen();
?>